                            function [ber,numBits] = bertool_dsss_bpsk(EbNo,maxNumErrs,maxNumBits,varargin)
%maxNumErrs,maxNumBits 最大误比特数和最大比特数

totErr = 0;  % Number of errors observed
numBits = 0; % Number of bits processed

% --- Set up the simulation parameters. ---
%% --- INSERT YOUR CODE HERE.
interpolation_rate =8;         %内插系数
w_carrier = 0.25;               %载波数字频率FC/FS
Sample_position = 0;            %最佳采样点
data_length =1000;         %测试数据长度 maxNumBits
M = 2;         % DBPSK is binary.
 %扩频的snr与ebn0转换
%
%根升余弦滤波器
alpha = 0.35;
span = 6;
hrc = rcosdesign(alpha,span,interpolation_rate,'sqrt');  
n = 3;
% Simulate until the number of errors exceeds maxNumErrs
% or the number of bits processed exceeds maxNumBits.
while((totErr < maxNumErrs) && (numBits < maxNumBits))
    if isBERToolSimulationStopped(varargin{:})
      break
    end
    
    data_send = randi([0,1],data_length,1); %0/1序列
    data_bio = 1-2*data_send; %对极序列
    
    
    %%扩频
    PN = mseq(n,[1 3], [1 1 1], 1);
    PN =1-2*PN; %对极m序列
    LPN = length(PN);%计算PN码的长度，即为扩频比
    
    PN2 = fliplr(PN);%翻转PN序列
    
    data_upsample_pn = upsample(data_bio,LPN); 
    DS_data = conv(data_upsample_pn,PN); %完成直扩
    
    DS_data_upsample = upsample(DS_data,interpolation_rate);%内插
    
    %成型滤波
    baseband = conv(DS_data_upsample,hrc); 
    carrier = cos(2*pi*w_carrier*[0:length(baseband)-1]); %载波
    send_ds_bpsk = baseband'.*carrier; %载波调制
     snr = EbNo +10*log10(2)-10*log10(interpolation_rate)-10*log10(LPN);
    rece_DS_bpsk = awgn(send_ds_bpsk,snr,'measured');%加噪
    recv_carrier = rece_DS_bpsk.*carrier; %解调
    %匹配滤波
    recv_data = conv(recv_carrier,hrc); 
    
    
    data_rDS = conv(recv_data,upsample(PN2,interpolation_rate)); %解扩——与本地PN码相关
   % plot(data_rDS);
    dalay= length(hrc)+(LPN-1)*interpolation_rate;
    data_rDS_eff = data_rDS(dalay:end-dalay+1);
   % plot(data_rDS_eff);
    rdata_bipolar = downsample(data_rDS_eff,interpolation_rate*LPN,0);
   % stem(rdata_bipolar);
    rdata = rdata_bipolar(1:data_length+1);
    %stem(data_bio);
    %码元判决
    res = zeros(1,data_length);
    for j = 1:data_length
        if rdata(j) < 0
        res(j) = 1;
        end
    end
    rece_databio = res';
    samebit = (rece_databio == data_send);
    error= data_length-length(find(samebit == 1));
   
    totErr = totErr + error;
    numBits = numBits + data_length;

end % End of loop

% Compute the BER.
ber = totErr/numBits;
end